#include"httplib.h"
#include<iostream>
#include"Log.hpp"
#include "add_contacts.pb.h"
using namespace httplib;
using namespace ns_log;

int main()
{
    Server server;
    server.Post("/contacts/add",[&server](const Request& req,Response& resp)
    {
         add_contact::AddContactResponse add_resp;
        add_contact::AddContactRequest add_req;
        LOG(INFO,"收到一个post请求!\n");
        if(!add_req.ParseFromString(req.body))
        {
            LOG(ERROR,"反序列化失败!\n");
            add_resp.set_success(false);
            add_resp.set_uid("1");
            add_resp.set_error_desc("反序列化失败!\n");
            std::string out;
            add_resp.SerializeToString(&out);
            resp.body = out;
            resp.set_header("Content-Type","application/protobuf");
            return;
        }
        std::cout<<"------------新增联系人-------------"<<std::endl;
        std::cout<<"姓名: ";
        std::cout<<add_req.name()<<std::endl;
        std::cout<<"年龄: "<<add_req.age()<<std::endl;
        std::cout<<"-----------联系电话-------------"<<std::endl;
        for(int i = 0;i<add_req.phone_size();i++)
        {
            add_contact::AddContactRequest_Phone phone = add_req.phone(i);
            std::cout<<phone.number()<<std::endl;
            std::cout<<"电话类型: "<<phone.PhoneType_Name(phone.type())<<std::endl;
        }
        add_resp.set_success(true);
        add_resp.set_uid("1");
        std::string out;
        add_resp.SerializeToString(&out);
        resp.body = out;
        resp.set_header("Content-Type","application/protobuf");
    });
    server.listen("0.0.0.0",8888);
    return 0;
}